package com.ruoyi.quartz.task;
import java.math.BigDecimal;
import java.util.Date;
import com.google.common.collect.Maps;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.business.service.IBProductService;
import com.ruoyi.market.config.MarketUserConfig;
import com.ruoyi.market.util.HttpConnetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Author: ygw
 * @Date: 2022/06/11/17:00
 * @Description:
 * 定时推送产品信息至营销系统
 */
@Slf4j
@Component("marketProductTask")
public class MarketProductTask {

    @Autowired
    private IBProductService productService;

    @Autowired
    MarketUserConfig marketUserConfig;

    private void marketProductTask(String intervalTime) {
        //1.查询当前需要推送的数据
        List<ProductInfo> productInfos = productService.selectProductListByTime(Integer.valueOf(intervalTime));
        log.info("-------- 本次推送数量： " + productInfos.size());
        //2.组装数据
        if (productInfos.size() > 0) {
            JSONObject jsonParam = productInFormatt(productInfos);
            if(jsonParam.getJSONArray("data").size()>0){
                String resultInfo = null;
                try {
                    //3.推送任务
                    resultInfo = HttpConnetUtil.sendPost(marketUserConfig.getMarketProductImportUrl(), jsonParam.toJSONString());
                } catch (Exception e) {
                    log.error("--------- 推送至营销系统 异常",e);
                }
                log.info("-------- 营销用户推送至 营销系统： " + resultInfo);
            }
        }
    }

    /**
     * @param productInfos
     * @return
     * 组装产品信息数据
     */
    private JSONObject productInFormatt(List<ProductInfo> productInfos) {
        JSONObject jsonParam = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ProductInfo productInfo : productInfos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("productCode",productInfo.getProductCode());
            jsonObject.put("productName",productInfo.getProductName());
            jsonObject.put("productType",productInfo.getProductType());
            jsonObject.put("creditRating",productInfo.getCreditRating());
            jsonObject.put("periodNum",productInfo.getPeriodNum());
            jsonObject.put("showInterestRate",productInfo.getShowInterestRate());
            jsonObject.put("showServiceRate",productInfo.getShowServiceRate());
            jsonObject.put("isDeferred",productInfo.getIsDeferred());
            jsonObject.put("tags",productInfo.getTags());
            jsonObject.put("deferredMaxDays",productInfo.getDeferredMaxDays());
            jsonObject.put("interestRate",productInfo.getInterestRate());
            jsonObject.put("serviceFee",productInfo.getServiceFee());
            jsonObject.put("isSinglePayment",productInfo.getIsSinglePayment());
            jsonObject.put("creditScale",productInfo.getCreditScale());
            jsonObject.put("minQuota",productInfo.getMinQuota());
            jsonObject.put("maxQuota",productInfo.getMaxQuota());
            jsonObject.put("startDate",productInfo.getStartDate());
            jsonObject.put("endDate",productInfo.getEndDate());
            jsonObject.put("dateLateFee",productInfo.getDateLateFee());
            jsonObject.put("lateFeeGracePeriod",productInfo.getLateFeeGracePeriod());
            jsonObject.put("lateFeeMaxDays",productInfo.getLateFeeMaxDays());
            jsonObject.put("state",productInfo.getState());
            jsonObject.put("defult",productInfo.getDefult());
            jsonArray.add(jsonObject);
        }
        jsonParam.put("data",jsonArray);
        return jsonParam;
    }
}
